package accountManagement.servlet;

import java.io.IOException;
import java.security.InvalidParameterException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import accountManagement.Account;
import accountManagement.AccountManager;
import accountManagement.Admin;
import accountManagement.Client;
import accountManagement.Employee;

/**
 * Servlet implementation class ServletCreaAccount
 */
@WebServlet("/ServletCreateAccount")
public class ServletCreateAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletCreateAccount() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

    private void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String type = request.getParameter("type");
    		
			Object user = request.getSession().getAttribute("user");
	    	if((type.equals("client") && user != null) ||
	    			((type.equals("employee") || type.equals("admin")) && (user == null || !(user instanceof Admin)))) {
	    		throw new InvalidParameterException("Non disponi dei privilegi necessari per accedere alla pagina richiesta");
	    	}
	    	
			String nickname = request.getParameter("nickname");
    		String password = request.getParameter("password");
    		
    		Account account;
    		switch(type) {
	    		case "client" :
					// Dati di Cliente
					String surname = request.getParameter("surname");
					String name = request.getParameter("name");
					String fiscalCode = request.getParameter("fiscalCode");
					account = new Client(nickname, password, surname, name, fiscalCode);
					break;
				case "employee" :
					String employeeId = request.getParameter("employeeId");
					account = new Employee(nickname, password, employeeId);
					break;
				case "admin"	:
					account = new Admin(nickname, password);
					break;
				default	:
					account = new Account(nickname, password);
			}
    		
    		AccountManager.getInstance().add(account);
    		request.setAttribute("resultTitle", "Creazione account " + type);
			request.setAttribute("resultContent", "Account creato");
			request.getRequestDispatcher("/result.jsp").forward(request, response);
		} catch(InvalidParameterException | NullPointerException e) {
			request.setAttribute("error", e.getMessage());
    		request.getRequestDispatcher("/errorPage.jsp").forward(request, response);
		}
    }
}
